﻿@model HanGu.Entity.Role
@{
    Layout = null;
    var Modules = ViewData["Modules"] as List<HanGu.Entity.Module>;
    var Roles = ViewData["Roles"] as List<HanGu.Entity.VRolePurview>;
    var CurentUser = Session["CurentUser"] as HanGu.Entity.CurrentUserDTO;
    var i = 1;
    var SelectedRoles = Roles.Count() > 0 ? string.Join(",", Roles.Select(c => c.ID).ToList()) : string.Empty;
}
<div class="pageContent">
    <style>
        .tbleFirstcat
        {
            background-color: #999;
            font-weight: bold;
            color: #fff;
        }
        .tbleFirstcat:hover
        {
            color: #000;
        }
        .tbleSecondcat
        {
            background-color: #ccc;
            font-weight: bold;
            color: #fff;
        }
        .tbleSecondcat:hover
        {
            color: #000;
        }
    </style>
    <form method="post" action="Roles/Default/create?OpID=@ViewData["OperateID"]" class="pageForm required-validate"
    onsubmit="return validateCallback(this, navTabAjaxDone);">
    @Html.ValidationSummary(true)
    <div class="pageFormContent" layouth="56">
        @Html.HiddenFor(model => model.ID)
        <input id="SelectedRoles" name="SelectedRoles" type="hidden" value="@SelectedRoles" />
        <p>
            <label>
                角色名称：</label>
            @Html.TextBoxFor(model => model.Name, new { @class = "required" })
            @Html.ValidationMessageFor(model => model.Name)
        </p>
        <p>
            <label>
                创建时间：</label>
            @Html.TextBoxFor(model => model.CreateTime, new { @Readonly = "true" })
            @Html.ValidationMessageFor(model => model.CreateTime)
        </p>
        <p>
            <label>
                创建者：</label>
            @Html.TextBoxFor(model => model.Creator, new { @Readonly = "true" })
            @Html.ValidationMessageFor(model => model.Creator)
        </p>
        <div class="divider">
        </div>
        <table class="list" width="100%" layouth="170">
            <thead>
                <th style="width: 30px">
                    序号
                </th>
                <th>
                    模块名称
                </th>
                <th>
                    模块与权限
                </th>
            </thead>
            @foreach (var parent in Modules.Where(c => string.IsNullOrEmpty(c.Parent)).OrderBy(c => c.Order))
            {
                i = 1;
                var isParentSelected = Roles.Where(c => c.ID == parent.ID).Count() > 0;       
                <tr class="tbleFirstcat" style="background-color:rgb(153, 153, 153)">
                    <td colspan="3">
                        <input type="checkbox" value="@parent.ID" name="@parent.ID" checked="@isParentSelected"  onclick="SelectingModules(this)"/><b>@parent.ModuleName</b>
                    </td>
                </tr>            
                foreach (var modules in Modules.Where(c => c.Parent == parent.ID).OrderBy(c => c.Order))
                {
                    var isModulesSelected = Roles.Where(c => c.ID == modules.ID).Count() > 0;                  
                <tr>
                    <td>
                        @i
                    </td>
                    <td title="@modules.Title">
                        <input type="checkbox" value="@modules.ID" name="@(parent.ID + "-" + @modules.ID)" checked="@isModulesSelected"  onclick="SelectingModules(this)"/>
                        @modules.ModuleName
                    </td>
                    <td>
                        <table>
                            <tr>
                                @foreach (var item in Modules.Where(c => c.Parent == modules.ID).OrderBy(c => c.Order))
                                {
                                    var isItemSelected = Roles.Where(c => c.ID == item.ID).Count() > 0; 
                                    <td>
                                        <input type="checkbox" value="@item.ID" name="@(parent.ID + "-" + item.Parent + "-" + @item.ID)" checked="@isItemSelected"  onclick="SelectingModules(this)"/>
                                        @item.ModuleName
                                    </td>}
                            </tr>
                        </table>
                    </td>
                </tr>
                        
                                i += 1;

                }
            }
        </table>
    </div>
    <div class="formBar">
        <ul>
            <li>
                <div class="buttonActive">
                    <div class="buttonContent">
                        <button type="submit">
                            提交</button></div>
                </div>
            </li>
            <li>
                <div class="button">
                    <div class="buttonContent">
                        <button type="button" class="close">
                            取消</button></div>
                </div>
            </li>
        </ul>
    </div>
    </form>
    <script>
        function SelectingModules(obj) {
            var $this = $(obj);
            var isChecked = obj.checked;
            var $value = $this.val();
            //选择父级后，全选择子级            
            $("input[name*='" + $value + "']").attr("checked", isChecked);
            //选择某子级，同时应选择其父级           
            var tmpValue = $this.attr("name");
            var parentValue = "";
            while (tmpValue.indexOf('-') >= 0) {
                var index = tmpValue.indexOf('-');
                var pValue = tmpValue.substring(0, index);
                parentValue = parentValue.length == 0 ? pValue : parentValue + "-" + pValue;
                //当取消选中时，若同级还有选中的，则父级还是选中
                var checked = isChecked ? isChecked : $("input[name*='" + parentValue + "']:checked").length > 0;
                $("input[name='" + parentValue + "']").attr("checked", checked);
                tmpValue = tmpValue.substring((index + 1));

            }
            var slectedValue = "";
            $("input:checked").each(function (item) {
                slectedValue += $(this).val() + ",";
            });
            $("#SelectedRoles").val(slectedValue);
        }
    </script>
</div>
